package gui;

import gui.GUIConstants.SelectionStatus;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Point;
import java.util.ArrayList;
import java.util.List;

import card.Card;
import card.CreatureCard;
import core.Battle;
import core.Constants;
import core.UpdateListener;
import core.Battle.BattleState;
import core.Constants.ConnectionType;

public class BattleView implements UpdateListener{
	private static BattleView me;
	private Battle battle;
	public List<SelectionSphere> selectionSpheres = new ArrayList<SelectionSphere>();
	public SelectionStatus selectionStatus = SelectionStatus.NO_SELECTION;
	private Card selectedCard;
	private int[][] damageTaken;
	public SimpleButton returnToMapButton = new SimpleButton(400, 0, 186, 96);
	public SimpleButton leftReadyButton = new SimpleButton(150, 5, 186, 96);
	public SimpleButton rightReadyButton = new SimpleButton(650, 5, 186, 96);
	private enum Side{LEFT, RIGHT};
	private List<AnimationInterface> animations = new ArrayList<AnimationInterface>();
	Graphics graphics;
	
	public SimpleButton setPlayerOneButton;
	public SimpleButton setPlayerTwoButton;
	
	public static BattleView getInstance(){
		if(me==null)
			me = new BattleView();
		return me;	
	}
	private BattleView(){
		battle=Battle.getInstance();
		
		
		if(Constants.connectionState == ConnectionType.DISCONNECTED){
			setPlayerOneButton = new SimpleButton(0, 0, 60, 100);
			setPlayerOneButton.setColor(Color.red);
			setPlayerTwoButton = new SimpleButton(60, 0, 60, 100);
			setPlayerTwoButton.setColor(Color.blue);
		}
		
	}
	
	protected void paint(Graphics g){
		graphics = g;
		g.setColor(Color.white);
		g.fillRect(0, 0, CenterPanelReader.getWidth(), CenterPanelReader.getWidth());
		drawSideColor(battle.getPlayer(0).getColor(), Side.LEFT, g);
		drawSideColor(battle.getPlayer(1).getColor(), Side.RIGHT, g);
		for(SelectionSphere s : selectionSpheres){
			s.paint(g);
		}
		
		for(int i=0; i<=1; i++){
			for(int j=0; j<Constants.MAXCREATURESONTILE; j++){
				if(battle.getAttackOrder()[i][j] != -1){
					g.setColor(Color.black);
					Point p1=getPosition(i, j);
					Point p2 = getPosition(Battle.otherPlayerId(i), battle.getAttackOrder()[i][j]);				
					
					drawArrow(p1, p2, battle.getPlayer(i).getColor(), g);
				}
				
				if(damageTaken != null && damageTaken[i][j] != 0){
					if(battle.getCreatures(i).get(j).getCurrentHealth()>0)
						g.setColor(Color.green);
					else
						g.setColor(Color.black);
					Point pd=getPosition(i, j);
					g.setFont(new Font(null, 0, 30));
					g.drawString(""+damageTaken[i][j], pd.x, pd.y);
						
				}
			}

		}
		
		
		if(battle.battleState == BattleState.HASBATTLED){

			returnToMapButton.setImage("mapButton.png");
			returnToMapButton.paint(g);	
		}
		
		
		if(battle.getIsReadyToFight(0))
			leftReadyButton.setImage("readyButtonPressed.png");
		else
			leftReadyButton.setImage("readyButton.png");
		if(battle.getIsReadyToFight(1))
			rightReadyButton.setImage("readyButtonPressed.png");
		else
			rightReadyButton.setImage("readyButton.png");
			
		leftReadyButton.paint(g);
		rightReadyButton.paint(g);
		
		if(Constants.connectionState == ConnectionType.DISCONNECTED){
			setPlayerOneButton.paint(g);
			setPlayerTwoButton.paint(g);
		}
	}
	
	private void drawSideColor(Color c, Side side, Graphics g){
		int startWidth = 15;
		int currentX;
		if(side == Side.LEFT)
			currentX = 0;
		else
			currentX = CenterPanelReader.getWidth()-10;
		g.setColor(c);
		for(int w = startWidth; w>0; w--){
			g.fillRect(currentX, 0, w, CenterPanelReader.getHeight());
			if(side == Side.LEFT)
				currentX = currentX + w+1;
			else
				currentX = currentX - w;
			
		}
		
		
	}
	
	public void battleStarted(){
		selectionSpheres.clear();
		
		
		List<CreatureCard> cList0 = battle.getCreatures(0);
		List<CreatureCard> cList1 = battle.getCreatures(1);
		for(CreatureCard c : cList0){
			SelectionSphere s = new SelectionSphere(c);
			s.setPosition(getPosition(0, cList0.indexOf(c)));
			selectionSpheres.add(s);
		}
		for(CreatureCard c : cList1){
			SelectionSphere s = new SelectionSphere(c);
			s.setPosition(getPosition(1, cList1.indexOf(c)));
			selectionSpheres.add(s);
		}
		
	}
	
	/**
	 * return onscreen position of card coordinates
	 * @param x
	 * @param y
	 * @return
	 */
	private static Point getPosition(int x, int y){
		return new Point(x*600+200, y*150+150);
	}
	public void setSelectedCard(Card card) {
		selectedCard=card;
	}
	public Card getSelectedCard() {
		return selectedCard;	
	}
	public void setDamageTaken(int[][] dt){
		damageTaken=dt;
	}
	private void drawArrow(Point p1, Point p2, Color c, Graphics g){
		boolean arrowOrientationAbove = true;
		int startHeight = 20;
		int startOffset = 30;
		
		if(p1.x < p2.x){
			arrowOrientationAbove=true;
		}
		else{
			arrowOrientationAbove=false;
		}
		
		g.setColor(c);
		int[] x = {p1.x, p1.x, p2.x};
		int[] y = new int[3];
		if(arrowOrientationAbove){
			y[0] = p1.y-startOffset;
			y[1] = p1.y-startOffset-startHeight;
			y[2] = p2.y;
		}
		else{
			y[0] = p1.y+startOffset;
			y[1] = p1.y+startOffset+startHeight;
			y[2] = p2.y;
		}
		g.fillPolygon(x, y, 3);
		
	}
	@Override
	public void update() {
		paint(graphics);		
	}

	
	
	
}
